package com.bestcem.xm.ticket.controller.vo.ticket.config;

import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.ticket.entity.mongo.TicketRule;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Data
public class TicketConfigRuleVO {
    @JsonProperty(value = "id")
    private String id;

    /**
     * 问卷id
     */
    @JsonProperty(value = "surveyId")
    private String surveyId;

    /**
     * 工单配置id
     */
    @JsonProperty(value = "ticketConfigId")
    private String ticketConfigId;

    /**
     * 规则标题
     */
    @JsonProperty(value = "title")
    private String title;

    /**
     * 同一问卷内，规则id唯一
     */
    @JsonProperty(value = "gid")
    private Integer gid;

    /**
     * 二次投放任务，工单完成或失败状态
     */
    @JsonProperty(value = "operation")
    private Integer operation;

    /**
     * 创建人
     */
    @JsonProperty(value = "creator")
    private String creator;

    /**
     * 版本号
     */
    @JsonProperty(value = "version")
    private Integer version;

    /**
     * 规则类型
     *
     * @see com.bestcem.xm.ticket.enums.TicketRuleTypeEnum
     */
    @JsonProperty(value = "rtype")
    private Integer type;

    /**
     * 工单生成条件
     * 字段类型，需要确认具体字段
     */
    @JsonProperty(value = "condition")
    private Condition condition;

    @JsonIgnore
    @JsonProperty(value = "targetList")
    private List<Target> targetList;

    /**
     * 生成条件列表
     */
    @JsonIgnore
    @JsonProperty(value = "conditionList")
    private List<Condition> conditionList;

    @Data
    public static class Condition {
        @JsonProperty(value = "logic")
        private Integer logic;

        @JsonProperty(value = "filters")
        private List<JSONObject> filters;

        @JsonProperty(value = "ruleType")
        private Integer ruleType;

        @JsonProperty(value = "rule")
        private String rule;

        @Nullable
        public static Condition getInstance(TicketRule.Condition entity) {
            if (Objects.isNull(entity)) {
                return null;
            }
            Condition data = new Condition();
            data.setLogic(entity.getLogic());
            data.setRuleType(entity.getRuleType());
            data.setFilters(entity.getFilters());
            data.setRule(entity.getRule());

            return data;
        }
    }


    @Data
    public static class Target {

        @JsonProperty(value = "levelID")
        private String levelID;

        @JsonProperty(value = "roleID")
        private String roleID;

        @JsonProperty(value = "roleOptions")
        private List<RoleOption> roleOptions;

        @Data
        public static class RoleOption {
            @JsonProperty(value = "id")
            private String id;

            @Nullable
            public static RoleOption getInstance(TicketRule.Target.RoleOption entity) {
                if (Objects.isNull(entity)) {
                    return null;
                }
                RoleOption data = new RoleOption();
                data.setId(entity.getId());
                return data;
            }
        }

        @Nullable
        public static Target getInstance(TicketRule.Target entity) {
            if (Objects.isNull(entity)) {
                return null;
            }
            Target data = new Target();
            data.setLevelID(entity.getLevelID());
            data.setRoleID(entity.getRoleID());

            List<RoleOption> roleOptionList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(entity.getRoleOptions())) {
                List<TicketRule.Target.RoleOption> roleOptions = entity.getRoleOptions();
                for (TicketRule.Target.RoleOption roleOption : roleOptions) {
                    roleOptionList.add(RoleOption.getInstance(roleOption));
                }
            }
            data.setRoleOptions(roleOptionList);

            return data;
        }
    }

    @JsonProperty(value = "record_flag")
    private Integer recordFlag = 1;

    @JsonProperty("created_id")
    private String createdId;

    @JsonProperty("created_dt")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createdDate;

    @JsonProperty("updated_id")
    private String updatedId;

    @JsonProperty("updated_dt")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date updatedDate;
}
